package com.qunar.mina.app.utils;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

public class JdbcUtils {
	private static String url;
	private static String driver;
	private static String user;
	private static String password;

	static {
		Properties props = new Properties();
		try {
			props.load(JdbcUtils.class.getClassLoader().getResourceAsStream("com/qunar/mina/app/config/jdbc.properties"));
		} catch (IOException e) {
			e.printStackTrace();
		}

		driver = props.getProperty("driver");
		url = props.getProperty("url");
		user = props.getProperty("user");
		password = props.getProperty("password");
		System.out.println(driver);
		try {
			Class.forName(driver);
		} catch (ClassNotFoundException e) {
			throw new RuntimeException(e);
		}

	}

	// 返回Connection
	public static Connection getConnection() {
		try {
			Connection con = DriverManager.getConnection(url, user, password);
			return con;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public static void closeConnection(Connection conn) {
		if (conn != null) {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
	public static void release(Connection connection,PreparedStatement pstmt,
			ResultSet rs){
		try {
			if (rs != null) 
				rs.close();
			if (pstmt!=null)
				pstmt.close();
			if (connection != null)
				connection.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
	}
}
